import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class BigDecCalTest {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        while (true) {
            System.out.print("Calculator> ");
            String str = br.readLine();
            if (str.equals("exit")) {
                break;
            }
            String result = calculate(str);
            System.out.println(result);
        }
        br.close();
    }

    public static String calculate(String str) {
        where = 0;
        return f(str.toCharArray(), 0).toString();
    }

    public static int where;

    // s[i....]开始计算，遇到字符串终止 或者 遇到)停止
    // 返回 : 自己负责的这一段，计算的结果
    // 返回之前，更新全局变量where，为了调用自己的上一层函数知道从哪继续
    public static BigDecimal f(char[] s, int i) {
        BigDecimal cur = BigDecimal.ZERO;
        ArrayList<BigDecimal> numbers = new ArrayList<>();
        ArrayList<Character> ops = new ArrayList<>();
        while (i < s.length && s[i] != ')') {
            if (s[i] >= '0' && s[i] <= '9') {
                cur = cur.multiply(BigDecimal.TEN).add(BigDecimal.valueOf(s[i++] - '0'));
            } else if (s[i] == '.') {
                i++;
                BigDecimal fraction = BigDecimal.ZERO;
                BigDecimal divisor = BigDecimal.TEN;
                while (i < s.length && s[i] >= '0' && s[i] <= '9') {
                    fraction = fraction.add(BigDecimal.valueOf(s[i++] - '0').divide(divisor, MathContext.DECIMAL128));
                    divisor = divisor.multiply(BigDecimal.TEN);
                }
                cur = cur.add(fraction);
            } else if (s[i] != '(') {
                // 遇到了运算符 + - * /
                push(numbers, ops, cur, s[i++]);
                cur = BigDecimal.ZERO;
            } else {
                // i (.....)
                // 遇到了左括号！
                cur = f(s, i + 1);
                i = where + 1;
            }
        }
        push(numbers, ops, cur, '+');
        where = i;
        return compute(numbers, ops);
    }

    public static void push(ArrayList<BigDecimal> numbers, ArrayList<Character> ops, BigDecimal cur, char op) {
        int n = numbers.size();
        if (n == 0 || ops.get(n - 1) == '+' || ops.get(n - 1) == '-') {
            numbers.add(cur);
            ops.add(op);
        } else {
            BigDecimal topNumber = numbers.get(n - 1);
            char topOp = ops.get(n - 1);
            if (topOp == '*') {
                numbers.set(n - 1, topNumber.multiply(cur));
            } else {
                numbers.set(n - 1, topNumber.divide(cur, MathContext.DECIMAL128));
            }
            ops.set(n - 1, op);
        }
    }

    public static BigDecimal compute(ArrayList<BigDecimal> numbers, ArrayList<Character> ops) {
        int n = numbers.size();
        BigDecimal ans = numbers.get(0);
        for (int i = 1; i < n; i++) {
            if (ops.get(i - 1) == '+') {
                ans = ans.add(numbers.get(i));
            } else {
                ans = ans.subtract(numbers.get(i));
            }
        }
        return ans;
    }

    // 处理除法结果，检测循环小数并标记
    public static String handleDivision(BigDecimal numerator, BigDecimal denominator) {
        if (denominator.compareTo(BigDecimal.ZERO) == 0) {
            throw new ArithmeticException("Division by zero.");
        }

        BigDecimal result = numerator.divide(denominator, MathContext.DECIMAL128);
        String resultStr = result.toPlainString();

        // 检测循环小数
        if (resultStr.contains(".")) {
            String[] parts = resultStr.split("\\.");
            String integerPart = parts[0];
            String decimalPart = parts[1];

            // 检测循环节
            Map<String, Integer> remainderMap = new HashMap<>();
            StringBuilder decimalBuilder = new StringBuilder();
            BigDecimal remainder = numerator.remainder(denominator);
            int index = 0;

            while (remainder.compareTo(BigDecimal.ZERO) != 0) {
                String remainderStr = remainder.toString();
                if (remainderMap.containsKey(remainderStr)) {
                    int start = remainderMap.get(remainderStr);
                    String nonRepeating = decimalBuilder.substring(0, start);
                    String repeating = decimalBuilder.substring(start);
                    return integerPart + "." + nonRepeating + "[" + repeating + "]";
                }

                remainderMap.put(remainderStr, index++);
                remainder = remainder.multiply(BigDecimal.TEN);
                BigDecimal digit = remainder.divide(denominator, RoundingMode.DOWN);
                decimalBuilder.append(digit.toPlainString());
                remainder = remainder.remainder(denominator);
            }

            return resultStr;
        }

        return resultStr;
    }
}